define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'date/index' + location.search,
                    add_url: 'date/add?venue_id='+ Fast.api.query('venue_id'),
                    // edit_url: 'date/edit?venue_id='+ Fast.api.query('venue_id'),
                    del_url: 'date/del',
                    multi_url: 'date/multi',
                    table: 'date',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'date', title: __('Date'), operate: false},
                        {field: 'venue_id', title: __('Venue'), searchList: Config.venueList, formatter: Table.api.formatter.label},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {1: __('Bookable'), 2: __('Non bookable')}, formatter: Table.api.formatter.toggle,yes:1,no:2},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            //批量添加日期
            $('.btn-date').click(function (){
                layer.confirm(__('确认追加15条日期？'),{
                    icon:3,
                    btn: ['确认', '取消']
                },function (index){
                    Fast.api.ajax({
                        url:'date/batchadd',
                        data:{venue_id:Config.venue_id}
                    },function (data,ret) {
                        parent.Toastr.success("追加成功！");//成功后提示
                        $("a.btn-refresh").trigger("click");
                        layer.close(index);
                        return false;
                    },function (data,ret) {
                        parent.Toastr.error(ret.msg);
                        layer.close(index);
                        return false;
                    });
                },function (index){
                    layer.close(index);
                });
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});